package dbmanager.connectionManager;

import java.awt.Component;
import java.awt.PopupMenu;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import dbmanager.connectionManager.menu.AddConnectionItem;
import dbmanager.connectionManager.menu.AddFolderItem;
import dbmanager.struct.ConnectionFolder;
import dbmanager.struct.DBConnection;

public class TreePopup extends MouseAdapter {
	private JTree tree;
	private Component alternative;

	public TreePopup(JTree tree, Component alternative) {
		this.tree = tree;
		this.alternative = alternative;
	}

	private void myPopupEvent(MouseEvent e) {
		TreePath path = tree.getPathForLocation(e.getX(), e.getY());
		Rectangle pathBounds = tree.getUI().getPathBounds(tree, path);
		if (pathBounds != null && pathBounds.contains(e.getX(), e.getY())) {
			getPopupMenu(false, true).show(tree, pathBounds.x, pathBounds.y + pathBounds.height);
		} else {
			getPopupMenu(false, false).show(alternative, 10, e.getY());
		}
	}

	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger()) {
			myPopupEvent(e);
		} else {
			TreePath path = tree.getPathForLocation(e.getX(), e.getY());
			Rectangle pathBounds = tree.getUI().getPathBounds(tree, path);
			if (pathBounds == null || !pathBounds.contains(e.getX(), e.getY())) {
				tree.setSelectionPath(null);
			} else if (pathBounds != null && pathBounds.contains(e.getX(), e.getY())) {
				TreePath selectedPath = tree.getSelectionPath();
				DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
				if (parent.getUserObject() instanceof ConnectionFolder) {
					ConnectionFolder selectedItem = (ConnectionFolder) parent.getUserObject();
					System.out.println("Selected con:" + selectedItem.getFoldername());
				} else if (parent.getUserObject() instanceof DBConnection) {
					DBConnection selectedItem = (DBConnection) parent.getUserObject();
					System.out.println("Selected con:" + selectedItem.getConName());
				}
			}
		}
	}

	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			myPopupEvent(e);
	}

	private JPopupMenu getPopupMenu(boolean connectionSelected, boolean folderSelected) {

		JPopupMenu popupMenu = new JPopupMenu();
		if (!connectionSelected) {
			JMenu mnAdd = new JMenu("Add");
			popupMenu.add(mnAdd);
			AddFolderItem mntmAddConnectionFolder = new AddFolderItem(tree);
			mnAdd.add(mntmAddConnectionFolder);

			AddConnectionItem mntmAddDatabaseConection = new AddConnectionItem(tree);
			mnAdd.add(mntmAddDatabaseConection);
		}
		// only add a delete button, if a connection or a folder was selected
		if (connectionSelected || folderSelected) {
			JMenuItem mntmDelete = new JMenuItem("Delete");
			popupMenu.add(mntmDelete);
		}

		return popupMenu;
	}
}
